package com.elm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.elm.po.Food;
import com.elm.po.OrderDetailet;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
@Mapper
public interface OrderDetailetMapper {
	@Insert("<script>" +
	        "INSERT INTO orderdetailet (order_id, food_id, quantity) VALUES " +
	        "<foreach collection='list' item='od' separator=','>" +
	        "(#{od.order_id}, #{od.food_id}, #{od.quantity})" +
	        "</foreach>" +
	        "</script>")
	int saveOrderDetailetBatch(List<OrderDetailet> orderDetailetList);

	
	@Select("SELECT f.* FROM food f " +
	        "INNER JOIN orderdetailet od ON f.id = od.food_id " +
	        "WHERE od.order_id = #{orderId}")
	List<Food> getAllFoodByOrder(Integer orderId);

	@Select("SELECT " +
	        "od.*, " +
	        "f.id AS f_id, f.food_name AS f_name, f.food_img AS f_img, f.food_price AS f_price " +
	        "FROM orderdetailet od " +
	        "LEFT JOIN food f ON od.food_id = f.id " +
	        "WHERE od.order_id = #{orderId}")
	@Results({
	    // 映射OrderDetailet自身字段
	    @Result(column = "id", property = "id"),
	    @Result(column = "order_id", property = "order_id"),
	    @Result(column = "food_id", property = "food_id"),
	    @Result(column = "quantity", property = "quantity"),
	    
	    // 映射关联的Food对象
	    @Result(column = "f_id", property = "food.id"),
	    @Result(column = "f_name", property = "food.foodName"),
	    @Result(column = "f_img", property = "food.foodImg"),
	    @Result(column = "f_price", property = "food.foodPrice")
	})
	List<OrderDetailet> getDetailetsByOrder(Integer orderId);
}
